package cn.changeforyou.web.blog.controller;

import cn.changeforyou.web.blog.po.Article;
import cn.changeforyou.web.blog.service.ArticleService;
import cn.changeforyou.web.nutz.common.MyPage;

import javax.validation.Valid;

import org.nutz.dao.QueryResult;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.adaptor.JsonAdaptor;
import org.nutz.mvc.annotation.*;


/**
 * 博客后台处理器
 *
 * @author zhyu
 */
@IocBean
@At("/articleBack")
@Ok("json:full")
public class ArticleBackController {

    @Inject
    private
    ArticleService articleService;

    /**
     * 分页查询
     */
    @At(value = "/list", methods = "OPTIONS")
    @GET
    public NutMap getArticlePage(@Param("..") MyPage pager) {
        QueryResult queryResult = articleService.getArticlePage(pager);
        return new NutMap("status", 0).setv("data", queryResult);
    }

    @At(value = "/saveOrUpdate", methods = "OPTIONS")
    @POST
    @AdaptBy(type = JsonAdaptor.class)
    public NutMap saveOrUpdate(@Valid @Param("..") Article article) {
        return new NutMap("status", 0).setv("data", articleService.saveOrUpdate(article));
    }

    @At(value = "/?", methods = "OPTIONS")
    @AdaptBy(type = JsonAdaptor.class)
    @DELETE
    public NutMap delete(Integer id) {
        articleService.delete(id);
        return new NutMap("status", 0);
    }


}
